package com.mate.cloud.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;

/**
 * 自定局部过滤器,用于打印日志
 *
 * @author cong zhi
 * @email 448341911@qq.com
 * @createDate 2023/3/17 14:32
 * @version: v1.0
 */
@Slf4j
@Component
public class LogPrintGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {

    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return (((exchange, chain) -> {
            //打印日志
            log.info("打印日志.....{},{}", config.getName(), config.getValue());
            ServerHttpRequest request = exchange.getRequest().mutate().build();
            return chain.filter(exchange.mutate().request(request).build());
        }));
    }
}
